import request from 'request';
import httpMethod from '../enum/httpMethod';

export default class HttpClient {
    // static async get<T,R>(request:T): Promise<R> {
    //     return await req(request.method,)
    // }

    static async req(method: httpMethod, url: string, headers: object, json: object): Promise<Object> {
        if (!headers) {
            headers['content-type'] = 'application/json;charset=utf-8';
        }
        return new Promise(function (resolve, reject) {
            request(
                {
                    uri: url,
                    method,
                    headers,
                    json
                },
                function (err, res, body) {
                    if (err) {
                        return reject(err);
                    }
                    if (typeof body === 'string') {
                        try {
                            return resolve(JSON.parse(body));
                        } catch (e) {
                            return reject(e);
                        }
                    } else {
                        return resolve(body);
                    }
                }
            );
        });
    }
}

